package com.luxuewen.sxa.handler;

import com.luxuewen.sxa.common.enums.ResponseEnum;
import com.luxuewen.sxa.common.exception.ApiException;
import com.luxuewen.sxa.utils.response.ApiResponse;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Optional;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ApiResponse<Void> exception(Exception e) {
        log.error("全局异常信息 ex={}", e.getMessage(), e);
        return ApiResponse.error(50000, e.getMessage());
    }

    @ExceptionHandler(ApiException.class)
    public ApiResponse<Void> apiException(ApiException e) {
        log.error("接口异常信息 ex={}", e.getMessage(), e);
        return ApiResponse.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ApiResponse<Void> missingServletRequestParameterException(MissingServletRequestParameterException e) {
        log.error("接口异常信息missing ex={}", e.getMessage(), e);
        return ApiResponse.fail(ResponseEnum.PARAM_ERROR.getCode(), "缺少必要参数: " + e.getParameterName());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ApiResponse<Void> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("接口异常信息no ex={}", e.getMessage(), e);
        BindingResult result = e.getBindingResult();
        FieldError error = result.getFieldError();

        return ApiResponse.fail(ResponseEnum.PARAM_ERROR.getCode(), null == error ? "参数异常，绑定对象问题！" : "参数异常: "+ error.getField() + error.getDefaultMessage());
    }

    @ExceptionHandler(BindException.class)
    public ApiResponse<Void> bindException(BindException e) {
        log.error("接口异常信息bind ex={}", e.getMessage(), e);
        BindingResult result = e.getBindingResult();
        FieldError error = result.getFieldError();
        return ApiResponse.fail(ResponseEnum.PARAM_ERROR.getCode(), null == error ? "参数异常，绑定有误！" : "参数异常: "+ error.getField() + error.getDefaultMessage());
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public ApiResponse<Void> constraintViolationException(ConstraintViolationException e) {
        log.error("接口异常信息vio ex={}", e.getMessage(), e);
        Optional<ConstraintViolation<?>> first = e.getConstraintViolations().stream().findFirst();
        return ApiResponse.fail(ResponseEnum.PARAM_ERROR.getCode(), first.isPresent() ? first.get().getMessage() : "参数异常，不符合要求！");
    }

//    Optional<ConstraintViolation<?>> first = ex.getConstraintViolations().stream().findFirst();
//        return ResponseVO.error(ResponseConstant.ERROR_CODE, first.isPresent() ? first.get().getMessage() : ResponseConstant.ERROR_MESSAGE, "");


}
